package jvs.vfs.tools;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

/**
 * 
 * @author qiangli
 * 
 */
public class ImageView {

	public static BufferedImage scale2(BufferedImage img, int width,
			int height, Object hint, int type) {
		try {
			int w = img.getWidth();
			int h = img.getHeight();

			while ((w != width || h != height)) {
				w = Math.max(Math.min(w / 2, width), width);
				h = Math.max(Math.min(h / 2, height), height);
				img = scale(img, w, h, hint, type);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return img;
	}

	public static BufferedImage clip(BufferedImage img, int w, int h) {
		try {
			int x0 = Math.max((img.getWidth() - w) / 2, 0);
			int y0 = Math.max((img.getHeight() - h) / 2, 0);
			w = Math.min(img.getWidth(), w);
			h = Math.min(img.getHeight(), h);
			img = img.getSubimage(x0, y0, w, h);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return img;
	}

	private static BufferedImage scale(BufferedImage img, int width,
			int height, Object hint, int type) {
		BufferedImage bi = new BufferedImage(width, height, type);
		Graphics2D g2 = bi.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
		g2.drawImage(img, 0, 0, width, height, null);
		g2.dispose();
		return bi;
	}

	public static void toenail(InputStream src, OutputStream dst, String format, double ratio) {
		try {
			BufferedImage bi = ImageIO.read(src);
			int w = (int) (ratio * bi.getWidth());
			int h = (int) (ratio * bi.getHeight());
			int type = (bi.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
					: BufferedImage.TYPE_INT_ARGB;
			bi = scale(bi, w, h, RenderingHints.VALUE_INTERPOLATION_BILINEAR,
					type);
			// RenderingHints.VALUE_INTERPOLATION_BICUBIC);
			ImageIO.write(bi, format, dst);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * scale down image specifying e.g 320-480 pixels for each dimension
	 * @param src
	 * @param dst
	 * @param format
	 * @param min
	 * @param max
	 * @return
	 */
	public static double thumbnail(InputStream src, OutputStream dst, String format,
			int min, int max) {
		double r = -1;
		try {
			BufferedImage bi = ImageIO.read(src);
			int w = bi.getWidth();
			int h = bi.getHeight();
			if (w > min && h > min) {
				double ratio = (double) w / (double) h;
				System.out.println("ratio: " + ratio);
				r = Math.max((double) min / (double) w, (double) min
						/ (double) h);
				h = (int) (h * r);
				w = (int) (w * r);
				//int type = (bi.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
				//		: BufferedImage.TYPE_INT_ARGB;
				int type = bi.getType();
				bi = scale2(bi, w, h,
						RenderingHints.VALUE_INTERPOLATION_BILINEAR, type);
			}
			bi = clip(bi, max, max);
			ImageIO.write(bi, format, dst);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return r;
	}

	private ImageView() {
	}
}
